package com.wisdom.view;


import org.sipdroid.sipua.ui.Receiver;
import org.sipdroid.sipua.ui.Settings;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;

import com.bluecall.sipua.R;
import com.wisdom.tools.CCPConfig;

public class HomeSettingActivity extends Activity implements OnClickListener{
//	private ListView moreList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_setting_page);
//		moreList=(ListView) findViewById(R.id.more_list);
//		HomeSettingAdapter adapter=new HomeSettingAdapter(this);
//		moreList.setAdapter(adapter);
//		moreList.setOnItemClickListener(new OnItemClickListener() {//为每一个item设置相应的响应  
//		public void onItemClick(AdapterView<?> parent, View view,
//		        int position, long id) {
//				Uri uri;
//				Intent it;
		
		
//                
//            }  
//        });  
	}
	
	@Override
	public void onClick(View v) {
		Uri uri;
		Intent it;
		switch (v.getId()) {
		case R.id.setting_login: //账户管理
        	startActivity(new Intent(HomeSettingActivity.this,SettingMyAccount.class));
            break;  
        case R.id.setting_pay://我要充值
        	  uri = Uri.parse(CCPConfig.Mobile_BasicUrl+"/paymobile/"+CCPConfig.UserId);  
        	  it = new Intent(Intent.ACTION_VIEW, uri);  
        	 startActivity(it);
        	//startActivity(new Intent(HomeSettingActivity.this,settingPaymentActivity.class));  
        	break;
        case R.id.setting_share:  
            //分享  
            Intent intent=new Intent(Intent.ACTION_SEND);  
            intent.setType("text/plain");  
            intent.putExtra(Intent.EXTRA_SUBJECT, "分享");  
            intent.putExtra(Intent.EXTRA_TEXT, "我要推荐给你可以挣钱的网络电话:"+CCPConfig.Mobile_BasicUrl+"/go/"+CCPConfig.UserId);  
            startActivity(Intent.createChooser(intent, getTitle()));  
            break;  
        case R.id.setting_callsetting: //拨打设置
        	startActivity(new Intent(HomeSettingActivity.this,DialSettingActivity.class));
            break;  
        case R.id.setting_help:  
            //帮助  
            //startActivity(new Intent(HomeSettingActivity.this,HelpActivity.class));  
        	 uri = Uri.parse(CCPConfig.Mobile_BasicUrl+"/help/"+CCPConfig.UserId);  
        	 it = new Intent(Intent.ACTION_VIEW, uri);  
        	 startActivity(it);
            break;  
        case R.id.setting_advice:  
            //意见反馈  
          //  AppConnect.getInstance(MoreActivity.this).showFeedback(); 
        	 uri = Uri.parse(CCPConfig.Mobile_BasicUrl+"/feedback/"+CCPConfig.UserId);  
        	 it = new Intent(Intent.ACTION_VIEW, uri);  
        	 startActivity(it);
            break;  
        case R.id.setting_exit://退出  
         //   AlertDlgUtil.AlertExit(MoreActivity.this);  
        	ExitApp();
			
            break;  
//        case R.id.setting:
//        	startActivity(new Intent(HomeSettingActivity.this,Sipdroid.class));
//        	//startActivity(new Intent(HomeSettingActivity.this,DialActivity.class));
//        	break;
        case R.id.setting_callback:
        	startActivity(new Intent(HomeSettingActivity.this,MakeCallbackActivity.class));
        	break;
		}
		
	}
	
	public  void ExitApp() {
		new LogoutAsyncTask(getApplicationContext()).execute();
		finish();
	}
	
	public static class LogoutAsyncTask extends AsyncTask<Void, Void, Void>{
		
	    private Context context;
	    public LogoutAsyncTask(Context context){
	        this.context = context;
	    }
	    
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			CCPConfig.setConfigProperties("user_id", "");
			System.out.println("user--logonout--id---->" + CCPConfig.getConfigProperties().getProperty("user_id"));
			Editor edit = PreferenceManager.getDefaultSharedPreferences(context).edit();
			edit.putBoolean(Settings.PREF_ON, false);
			edit.commit();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			Receiver.pos(true);
			Receiver.engine(context).halt();
			Receiver.mSipdroidEngine = null;
			Receiver.reRegister(0);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Intent home = new Intent(Intent.ACTION_MAIN);  
			home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);  
			home.addCategory(Intent.CATEGORY_HOME);  
			context.startActivity(home);
		}
		
	}


	
}
